<?php if($user->access!=0){ ?>
<div id="page_file">
<?php
	//code de sécu normalement jamais executer
	if (! in_array($_GET['page'],$typefiles) and $_GET['page'] != "search" ){
			exit();
	}
	$filetype=mysql_real_escape_string($_GET['page']);
	
	//cas ou l'utilisateur choisi le trie qu'il veut
	if ($_GET['server']=="choice"){
		?>
		<table id="table_choice_server">
			<caption id ="servertit" >Server ?</caption>
		<tr onclick="location.href='index.php?page=<?php print $_GET['page']; ?>&server=allserver'">
			<td><a href="index.php?page=<?php print $_GET['page']; ?>&server=allserver">All Server</a></td>
		</tr>
		<?php
		$sql_query=mysql_query("Select name,address from ".DATABASE_TABLE_SERVER." ORDER BY `name` ASC");
		while ($query = mysql_fetch_array($sql_query) ){
			echo "<tr onclick=\"location.href='index.php?page=".$_GET['page']."&server=".$query['name']."'\" >";
						echo "<td name=\"".Server::mise_en_forme_address($query['address'])."\" ><a href=\"index.php?page=".$_GET['page']."&server=".$query['name']."\">".$query['name']."</a></td>";
			echo '</tr>';
		}
		?> </table><?php
	}
	elseif ($_GET['server']!=""){
		if ($_GET['server']=="allserver"){
			$server="%";
		}
		else{
			$server=mysql_real_escape_string(htmlspecialchars($_GET['server']));
		}
		
		//Pour info sur le serv(last update et name)
		$sql_query=mysql_query("Select name,last_update from ".DATABASE_TABLE_SERVER." WHERE name = '".$server."' ORDER BY `name` ASC");
		while ($query = mysql_fetch_array($sql_query) ){
				$base_server_name = $query['name'];
				$base_server_last_update = $query['last_update'];
		}
		
		
		//check trie des servers
		if (isset($_GET['sortby']) and $_GET['sortby'] ){
			$sortby = mysql_escape_string($_GET['sortby']);	
		}
		else{
			$sortby = mysql_escape_string('name');
		}
		
		
		if (isset($_GET['orderby']) and $_GET['orderby'] != "" ){
			$orderby = mysql_escape_string($_GET['orderby']);	
		}
		else{
			$orderby = mysql_escape_string('ASC');
		}
		
		
		
		if($_GET['page'] == "Other"){
			$sql_query=mysql_query("SELECT ".DATABASE_TABLE_FILE.".id,".DATABASE_TABLE_FILE.".fullpath,
									".DATABASE_TABLE_FILE.".type,".DATABASE_TABLE_FILE.".name, ".DATABASE_TABLE_FILE.".taille,
									".DATABASE_TABLE_FILE.".date, ".DATABASE_TABLE_FILE.".server, ".DATABASE_TABLE_SERVER.".address 
									FROM ".DATABASE_TABLE_FILE." JOIN ".DATABASE_TABLE_SERVER." ON ".DATABASE_TABLE_FILE.".server = ".DATABASE_TABLE_SERVER.".name 
									WHERE ".DATABASE_TABLE_FILE.".server like '".$server."' 
									AND ".DATABASE_TABLE_FILE.".type like '$filetype'ORDER BY ".$sortby." ".$orderby.",name ");
			
		}
		elseif($_GET['page'] == "search"){
			if ($_GET['file'] == ""){
				$def_array[0] = '%';
			}
			else{
				$def_array=explode(" ",mysql_escape_string($_GET['file']));
			}
			if (isset($_GET['orderby']) and $_GET['orderby'] != "" ){
				$orderby = mysql_escape_string($_GET['orderby']);	
			}
			else{
				$orderby = mysql_escape_string('DESC');
			}
			
			if (isset($_GET['sortby']) and $_GET['sortby'] != "" ){
				$sortby = mysql_escape_string($_GET['sortby']);	
			}
			else{
				$sortby = mysql_escape_string('pertinence');
			}
			if (isset($_GET['type']) and $_GET['type']!= ''){
				$filetype =	mysql_real_escape_string($_GET['type']);
				if($_GET['type'] == 'alltype'){
					$filetype = '%';	
				}
			}else{
				$filetype = '%';	
			}
			//requete by Polaris and google
			$sql_query=mysql_query("SELECT ".DATABASE_TABLE_FILE.".id,".DATABASE_TABLE_SERVER.".address ,".DATABASE_TABLE_FILE.".fullpath,".DATABASE_TABLE_FILE.".type,
										".DATABASE_TABLE_FILE.".name,".DATABASE_TABLE_FILE.".taille,".DATABASE_TABLE_FILE.".server,".DATABASE_TABLE_FILE.".date,
										((".Sql::array_in_where_plus_clause(DATABASE_TABLE_FILE.'.name',$def_array).")*100/".count($def_array).") AS pertinence ,
										CONCAT(round((".Sql::array_in_where_plus_clause(DATABASE_TABLE_FILE.'.name',$def_array).")*100/".count($def_array)."),'%') AS found_count
										FROM ".DATABASE_TABLE_FILE." JOIN ".DATABASE_TABLE_SERVER." ON ".DATABASE_TABLE_FILE.".server = ".DATABASE_TABLE_SERVER.".name
										WHERE ( ".Sql::array_in_where_OR_clause(DATABASE_TABLE_FILE.'.name',$def_array)." ) AND ".DATABASE_TABLE_FILE.".server like '".$server."' AND ".DATABASE_TABLE_FILE.".type like '".$filetype."' 
										ORDER BY ".$sortby." ".$orderby);
					
		}else{
			$def_array	= $def_film;
			$sql_query=mysql_query("SELECT ".DATABASE_TABLE_FILE.".id,".DATABASE_TABLE_FILE.".fullpath,".DATABASE_TABLE_FILE.".type,
									".DATABASE_TABLE_FILE.".name, ".DATABASE_TABLE_FILE.".taille, ".DATABASE_TABLE_FILE.".date,
									".DATABASE_TABLE_FILE.".server, ".DATABASE_TABLE_SERVER.".address 
									FROM ".DATABASE_TABLE_FILE." JOIN ".DATABASE_TABLE_SERVER." ON ".DATABASE_TABLE_FILE.".server = ".DATABASE_TABLE_SERVER.".name 
									WHERE ".DATABASE_TABLE_FILE.".server like '".$server."' AND ".DATABASE_TABLE_FILE.".type like '$filetype' 
									AND (".Sql::array_in_where_clause(DATABASE_TABLE_FILE.".name",$def_array).") ORDER BY ".$sortby." ".$orderby.",name ");
			
		}
		
		?> 
		<table>
		<caption><img alt="refresh" src="./img/refresh.png" onclick="refresh_serv('<?php print $server; ?>');Effect.Puff(this);" />List of files of <?php print htmlspecialchars($_GET['server']); ?><?php if ($server != "%" ){?> Last update : <?php print date("d/m/y  G\hi\m",$base_server_last_update); } ?> (<?php print mysql_num_rows($sql_query);?> files)</caption>
		<tr>
			<td><a class="title" href="index.php?page=<?php print $_GET['page']; ?>&file=<?php print $_GET['file']; ?>&server=<?php print $_GET['server']; ?>&type=<?php print $_GET['type']; ?>&orderby=<?php if($orderby=='ASC'){print 'DESC';}else{print 'ASC';}?>&sortby=name">Name</a></td>
<?php if($_GET['page'] == "search"){?>
			<td><a class="title" href="index.php?page=<?php print $_GET['page']; ?>&file=<?php print $_GET['file']; ?>&server=<?php print $_GET['server']; ?>&type=<?php print $_GET['type']; ?>&orderby=<?php if($orderby=='ASC'){print 'DESC';}else{print 'ASC';}?>&sortby=pertinence">Pertinence</a></td>
<?php } ?>			
			<td><a class="title" href="index.php?page=<?php print $_GET['page']; ?>&file=<?php print $_GET['file']; ?>&server=<?php print $_GET['server']; ?>&type=<?php print $_GET['type']; ?>&orderby=<?php if($orderby=='ASC'){print 'DESC';}else{print 'ASC';}?>&sortby=taille">Size</a></td>
			<td><a class="title" href="index.php?page=<?php print $_GET['page']; ?>&file=<?php print $_GET['file']; ?>&server=<?php print $_GET['server']; ?>&type=<?php print $_GET['type']; ?>&orderby=<?php if($orderby=='ASC'){print 'DESC';}else{print 'ASC';}?>&sortby=date">Date</a></td>
			<td><a class="title" href="index.php?page=<?php print $_GET['page']; ?>&file=<?php print $_GET['file']; ?>&server=<?php print $_GET['server']; ?>&type=<?php print $_GET['type']; ?>&orderby=<?php if($orderby=='ASC'){print 'DESC';}else{print 'ASC';}?>&sortby=server">Server</a></td>
			<td><a class="title" href="index.php?page=<?php print $_GET['page']; ?>&file=<?php print $_GET['file']; ?>&server=<?php print $_GET['server']; ?>&type=<?php print $_GET['type']; ?>&orderby=<?php if($orderby=='ASC'){print 'DESC';}else{print 'ASC';}?>&sortby=type">Type</a></td>
<?php if($_GET['page'] == "Other" or $_GET['page'] == "Series"){?>
			<td><a class="title" href="index.php?page=<?php print $_GET['page']; ?>&file=<?php print $_GET['file']; ?>&server=<?php print $_GET['server']; ?>&type=<?php print $_GET['type']; ?>&orderby=<?php if($orderby=='ASC'){print 'DESC';}else{print 'ASC';}?>&sortby=fullpath">Folder</a></td>
<?php } ?>		
		</tr>
		<?php
		$arrayfav = Fav::fav_in_array($user->login);
		while ($query = mysql_fetch_array($sql_query) ){

				?>
				<tr bgcolor="#ffffff" onmouseout="this.bgColor='#FFFFFF';" onmouseover="this.bgColor='#D0E8F8';" >	
				<td>
					<?php if(! in_array($query['fullpath'],$arrayfav)){?>
						<img alt="add" title="add in your favorite list" src="./img/add_favorite.png" onclick="ajax_add_favorite('<?php print $query['id'] ?>');Effect.Puff(this);" />
					<?php }else{?>
						<img alt="rm" title="remove your favorite file" src="./img/rm_favorite.png" onclick="ajax_rm_favorite('<?php print $query['id'] ?>');Effect.Puff(this);" />
					<?php }?>
					<a target="_blank" href="http://www.allocine.fr/recherche/?motcle=<?php print File::rm_ext($query['name']) ?>&x=0&y=0&rub=0" ><img style="border:0;" src="img/allo.ico" alt="BA"   /></a>
					<a  href="index.php?film=<?php print md5($query['fullpath']); ?>&idserver=<?php print $query['server']; ?>&page=<?php print $_GET['page']; ?>&server=<?php print $_GET['server']; ?>" onmouseover="this.innerHTML=this.innerHTML+'<?php print htmlspecialchars(stripslashes(str_replace("'","\'",File::get_ext($query['name']))));?>'" onmouseout="this.innerHTML='<?php print htmlspecialchars(stripslashes(str_replace("'","\'",File::rm_ext($query['name']))));?>'"><?php print htmlspecialchars(stripslashes(File::rm_ext($query['name'])));?></a>
				</td>
				
<?php if($_GET['page'] == "search"){?>
				<td><?php print $query['found_count'];?></td>		
<?php } ?>				
				
				<td><?php print File::size_modif($query['taille']); ?></td>
				<td><?php print date("d/m/y",$query['date']);?></td>
				<td name="<?php print Server::mise_en_forme_address($query['address']);?>"><?php print $query['server'];?></td>
				<td><?php print $query['type'];?></td>
<?php if($_GET['page'] == "Other" or $_GET['page'] == "Series"){?>
				<td title="<?php print File::modify_parent_path($query['fullpath']); ?>"><?php print substr(File::modify_parent_path($query['fullpath']),0,20);?></td>		
<?php } ?>
				</tr>
				<?php

		}//fin du while
		?>
		</table>
<?php		
	}
?>
</div>
<?php
//gestion de la couleur des serveur down
	$sql_query=mysql_query("Select address from ".DATABASE_TABLE_SERVER." ORDER BY `name` ASC");
	$javascript_string="";
	while ($query = mysql_fetch_array($sql_query)){
		$javascript_string = $javascript_string."\"".Server::mise_en_forme_address($query['address'])."\",";
	}
	$javascript_string = substr($javascript_string,0,-1);
	?>
	<script>
		var arrayserv = new Array(<?php print $javascript_string; ?>);
		info_serv_up(arrayserv);
	</script>
	
<?php } ?>